//
//  LYRCircleImages.m
//  06-轮播图
//
//  Created by 402030900@qq.com on 16/7/21.
//  Copyright © 2016年 402030900@qq.com. All rights reserved.
//

#import "LYRCircleImages.h"

@interface LYRCircleImages()<UIScrollViewDelegate> //延展
@property(nonatomic,strong)UIScrollView * cycleScrollView;
@property(nonatomic,strong)UIPageControl * imagePage;
@end
@implementation LYRCircleImages
#pragma mark -scrollViewDelegate
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    // 改变pageControl的对应下标
    _imagePage.currentPage = scrollView.contentOffset.x / self.bounds.size.width;
    NSInteger page = scrollView.contentOffset.x / self.bounds.size.width;
    _imagePage.currentPage = page - 1;
        if (page == 0) {
            // 改变滚动视图的偏移量
            _cycleScrollView.contentOffset = CGPointMake(self.bounds.size.width * (_imagesArr.count - 2), 0);
            _imagePage.currentPage = _imagesArr.count - 3;
        }else if (page == _imagesArr.count - 1){
            _cycleScrollView.contentOffset = CGPointMake(self.bounds.size.width, 0);
            _imagePage.currentPage = 0;
        }
}
#pragma mark - init 初始化
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        // 子控件初始化时,frame不要与初始化方法的frame一样
        // 推荐使用 self.bounds / CGRectMake(0,0,frame.size.width,frame.size.height)
        self.backgroundColor = [UIColor yellowColor];
        [self initCycleScrollView];
        [self createImagePage:frame];
    }
    return self;
}
-(void)initCycleScrollView{
    _cycleScrollView = [[UIScrollView alloc]initWithFrame:self.bounds];
    _cycleScrollView.backgroundColor = [UIColor blackColor];
    _cycleScrollView.bounces = NO;
    _cycleScrollView.pagingEnabled = YES;
    _cycleScrollView.showsVerticalScrollIndicator = NO;
    _cycleScrollView.showsHorizontalScrollIndicator= NO;
    _cycleScrollView.delegate = self;
    [self addSubview:_cycleScrollView];
}
#warning 后期需要添加page的位置改变 这里是默认位置
- (void)createImagePage:(CGRect)frame{
    _imagePage = [[UIPageControl alloc]initWithFrame:CGRectMake(0, 0, frame.size.width / 3, 20)];
    _imagePage.numberOfPages = 5;
    _imagePage.backgroundColor = [UIColor clearColor];
    _imagePage.center = CGPointMake(frame.size.width / 2, frame.size.height - 10);
    [_imagePage addTarget:self action:@selector(imagePageChange:) forControlEvents:(UIControlEventValueChanged)];
    [self addSubview:_imagePage];

}
-(void)imagePageChange:(UIPageControl *)pageControl{
    [_cycleScrollView setContentOffset:CGPointMake((pageControl.currentPage + 1) * self.bounds.size.width, 0) animated:YES];

}
#pragma mark -ImagesArray
// 自定义方法
-(void)setImagesWithArray:(NSArray *)array{
    // 1.根据传过来的数组,进行处理,生成图片数组
   self .imagesArr = [self handleArray:array];
    // 2.逻辑判断时,需要用到数组属性
    self.imagesArr = _imagesArr;
    // 3.根据术语生成的对应的ImageView
    [self createAllImageViewsWithImageArray:_imagesArr];

    // 使用数组 创建 ImageView
       _imagePage.numberOfPages = array.count;
    _cycleScrollView.contentSize = CGSizeMake(_imagesArr.count * self.bounds.size.width, 0);
    // 添加最后一张图,要让轮播图看起来在第一张
    _cycleScrollView.contentOffset = CGPointMake(self.bounds.size.width, 0);
    
}
-(NSArray *)handleArray:(NSArray *)array{
    // 判断类型(不知道别人传进来的类型)
    id firstObject = [array firstObject];
    // 最终的图片数组
    NSMutableArray * imageArray = [NSMutableArray array];
    // 判断对象是不是某种类型 isKindOfClass:
    if ([firstObject isKindOfClass:[NSString class]]) {
        // 字符串数组 转成 图片数组
        for (NSString * imageName in array) {
            UIImage * image = [UIImage imageNamed:imageName];
            [imageArray addObject:image];
        }
    }else if ([firstObject isKindOfClass:[UIImage class]]){
        [imageArray setArray:array];
    }
    // 获取到第一张图
    // 获取到最后一张图
    UIImage * firstImage = [imageArray firstObject];
    UIImage * lastImage = [imageArray lastObject];
    // 下标为0的位置放最后一张图
    [imageArray insertObject:lastImage atIndex:0];
    // 在最后添加第一张图
    [imageArray addObject: firstImage];
    return imageArray;
    
}
-(void)createAllImageViewsWithImageArray:(NSArray *)imagesArray{
    for (int i = 0; i < _imagesArr.count; i ++) {
        UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(i * self.bounds.size.width, 0, self.bounds.size.width, self.bounds.size.height)];
        imageView.image = _imagesArr[i];
        [_cycleScrollView addSubview:imageView];
    }


}
@end
